<?php
/*
$JA#COPYRIGHT$
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.helper');

class modJAFilterJobsHelper {
	
	var $_fieldlist;		
	
	function check_multi_level($item)
	{
		if (isset($item->parent_id))
			return true;
		else 
			return false;
	}
	
	function getValues($fieldEditList){		
		if(!$fieldEditList) return array();
		
		require_once("components/com_jajobboard/models/jacodes.php");
	    $model_codes = new JAJobBoardModelJACodes();
	    
		$list = array();
		if($fieldEditList){
		    foreach ($fieldEditList as $field){
		    	if(is_object($field)){
			    	if( isset($field->is_referred) && $field->is_referred){
						$list[] = $this->getReferenceValue($field); 
				    } else {
				    	$field->table_name = isset($field->table_name)?$field->table_name:'';
				    	JRequest::setVar('field_id', $field->table_name.'_'.$field->field_name);	
						$list[] = $model_codes->getItems();
				    }
		    	}else $list[] = "";
		    }
		}
		return $list;
	}	
	
    function getReferenceValue($field){
       
        $db = &JFactory::getDBO();
        $key = $field->referred_key;
        $value = $field->referred_value;
        $table = $field->referred_table;
        
        $query = "SELECT *  FROM #__$table WHERE published=1 ORDER BY $field->referred_value ASC";
                    
        $db->setQuery($query);
        $objects = $db->loadObjectList();
        
        return $objects;   
    }        
	function dislay_singLevel_Select($items,$field,$jobalert)
	{
		$arr_filter_active = JRequest::getVar('arr_filter_active');
	    $selected_item = JRequest::getVar($field->field_name);
	    	    
	    if (!is_array($selected_item))
    		$selected_item = explode(',',$selected_item);
    	foreach ($items as $i=>$item)
    	{
    		$item->name = $item->text;
    		$is_active = false;
				if(isset($arr_filter_active[0])){
					for   ($j=0;$j<count($arr_filter_active[0]->value);$j++)
					{
					
						if 	($arr_filter_active[0]->value[$j]==$item->id)
						{
					?>  
												
					<input <?php echo (in_array($item->name,$selected_item))?'checked=checked':''; ?> title="<?php echo $item->name; ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id; ?>" value="<?php echo $item->id.' '.$item->name; ?>" onchange="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/>
					<?php
							$is_active = true;
							break;
							
						}
					}
				}
				if (!$is_active){
				?>
				<input <?php echo (in_array($item->name,$selected_item))?'checked=checked':''; ?> title="<?php echo $item->name; ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id; ?>" value="<?php echo $item->id.' '.$item->name; ?>" onchange="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/>
				<?php
					}
				?>
			
				<label class="hide"  for="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id; ?>">
					<?php echo $item->name;?>
				</label><br />
<?php
    	}
	}
    function display_mulLevel_Select($items,$field,$jobalert){
		
    	if ($jobalert)
    	{
    		if ($field->referred_table=="ja_location")
	    	{
				
	    		$selected_item = $jobalert->location;
	    	}
	    	elseif ($field->referred_table=="ja_categories")
	    	{
	    		$selected_item = $jobalert->category;
	    	}

    	}
    	else 
    	{
	    	$arr_filter_active = JRequest::getVar('arr_filter_active');
	    	$selected_item = JRequest::getVar($field->field_name);
    	}
    	if (!is_array($selected_item))
    		$selected_item = explode(',',$selected_item);
    		
    	if ($items)
        {
	        foreach ($items as $v )
	        {
	            $pt = $v->parent_id ? $v->parent_id : 0 ;            
	            $list = @$cat_children[$pt] ? $cat_children[$pt] : array();
	            array_push( $list, $v );
	            $cat_children[$pt] = $list;
	        }
        }
        
        // second pass - get an indent list of the items
        $list = $this->treerecurse(0, "", array(), $cat_children, 9999 );
        
        $treeItems     = array();
        $has_table_existed = false;
        $count_table = 0;
                
        foreach($list as $i => $item){
            $checked = isset($this->search["filter_categories"]) && in_array($item->id, $this->search["filter_categories"]) ? "checked='checked'" : "";
			
            $item->name = JText::_($item->name);
            			
            if ($item->parent_id==0){?>
            	<div>
            		
            					<?php  
            					echo $item->pretext;
            					$is_active = false;
								if(isset($arr_filter_active[0])){
									for   ($j=0;$j<count($arr_filter_active[0]->value);$j++)
									{
									
										if 	($arr_filter_active[0]->value[$j]==$item->id)
										{
									?>  
																
									<input tabindex="1" <?php echo (in_array($item->name,$selected_item))?'checked=checked':''; ?> title="<?php echo $item->name; ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>" value="<?php echo $item->id.' '.str_replace("&amp;", "--", $item->name); ?>" <?php echo $checked;?>  onclick="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/>
									<?php
											$is_active = true;
											break;
											
										}
									}
								}
            					if (!$is_active){
								?>
								<input  tabindex="2" <?php echo (in_array($item->name,$selected_item))?'checked=checked':''; ?> title="<?php echo $item->name; ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>" value="<?php echo $item->id.' '.str_replace("&amp;", "--", $item->name); ?>" <?php echo $checked;?> onclick="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/>
								<?php
            						}
								?>
							
								<label class="hide" for="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>">
									<?php echo str_replace("&amp;", "&", $item->name);?>
								</label>
							
				</div>								
			<?php }else {?>
				<div>
					<?php 
					echo $item->pretext;
					$is_active = false;
					if(isset($arr_filter_active[0])){
						for   ($j=0;$j<count($arr_filter_active[0]->value);$j++)
						{
							if 	($arr_filter_active[0]->value[$j]==$item->id)
							{
						?>          
				  
						<input  tabindex="3" <?php echo (in_array($item->name,$selected_item))?'checked=checked':''; ?> title="<?php echo $item->name; ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>" value="<?php echo $item->id.' '.$item->name; ?>" <?php echo $checked;?> onchange="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/> 
						<?php 
								$is_active = true;
								break;
							}
						}
					}
            		if (!$is_active)
            		{
            						
			 		?>
			 		<input tabindex="4" <?php echo (in_array( JText::_($item->name),$selected_item))?'checked=checked':''; ?> title="<?php echo JText::_($item->name); ?>" type="checkbox" name="filter_<?php echo $field->field_name; ?>[]" id="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>" value="<?php echo $item->id.' '.$item->name; ?>" <?php echo $checked;?> onchange="click_checkbox(<?php echo $item->id;?>,'<?php echo $field->field_name;?>');"/> 
			 		<?php
            		}
			 		?>
                    <label for="filter_<?php echo $field->field_name; ?>_list<?php echo $item->id.'_'.$item->parent_id; ?>">
                    	<?php echo str_replace("&amp;", "&", $item->name);?>
                    </label>
                </div>
                    
                <?php }?>   
               
			<?php
        }		
    }
        
    function treerecurse( $id, $indent, $list, &$children, $maxlevel=9999, $level=0, $type=1 )
    {
        if (@$children[$id] && $level <= $maxlevel)
        {
            foreach ($children[$id] as $v)
            {
                $id = $v->id;

                $pre = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level);
                
                $pt = $v->parent_id;
                $list[$id] = $v;
                $list[$id]->children = count( @$children[$id] );
                $list[$id]->pretext = $pre;
                $list = $this->treeRecurse( $id, $indent, $list, $children, $maxlevel, $level+1, $type );
            }
        }
        
        return $list;
    }    
    
    
    function getFile($name, $modPath, $tmplPath) {
		if (file_exists(JPATH_SITE.DS.$tmplPath.$name)) {
			return $tmplPath.$name;
		}
		return $modPath.$name;
	}
	    
}